package com.liao.starapp.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class GetAssetsData {

    private static Map<String, Bitmap> contentLogoImgMap;
    private static Map<String, Bitmap> logoImgMap;

    public static String getGridViewText(Context context, String fileName) {
        try {
            InputStream is = context.getAssets().open(fileName);
            int length = is.available();
            byte[] bytes = new byte[length];
            is.read(bytes);
            String s = new String(bytes);
            return s;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Bitmap getGridViewImg(Context context, String fileName) {
        try {
            InputStream is = context.getAssets().open(fileName);
            return BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void saveAssetsImg(Context context) {
        AssetManager assets = context.getAssets();
        logoImgMap = new HashMap<>();
        contentLogoImgMap = new HashMap<>();
        try {
            String[] xzcontentlogos = assets.list("xzcontentlogo");
            String[] xzlogos = assets.list("xzlogo");
            if (xzcontentlogos != null) {
                for (String xzcontentlogo : xzcontentlogos) {
                    InputStream open = assets.open("xzcontentlogo/" + xzcontentlogo);
                    Bitmap bitmap = BitmapFactory.decodeStream(open);
                    contentLogoImgMap.put(xzcontentlogo, bitmap);
                }
            }
            if (xzlogos != null) {
                for (String xzlogo : xzlogos) {
                    InputStream open = assets.open("xzlogo/" + xzlogo);
                    Bitmap bitmap = BitmapFactory.decodeStream(open);
                    logoImgMap.put(xzlogo, bitmap);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Map<String, Bitmap> getContentLogoImgMap() {
        return contentLogoImgMap;
    }

    public static Map<String, Bitmap> getLogoImgMap() {
        return logoImgMap;
    }
}
